<?php

/*
    User model
 */
class Model_Orm_User extends Orm\Model {
    protected static $_table_name = 'users';
    protected static $_primary_key = array('id');
    protected static $_properties = array(
	'id',
	'username' => array(
            'validation' => array('required'),
            'data_type' => 'varchar'
        ),
	'password' => array(
            'validation' => array('required'),
            'data_type' => 'varchar'
        ),
	'group' => array(
            'data_type' => 'int'
        ),
	'email' => array(
            'data_type' => 'varchar'
        ),
	'last_login' => array(
            'data_type' => 'varchar'
        ),
	'login_hash' => array(
            'data_type' => 'varchar'
        ),
	'profile_fields' => array(
            'data_type' => 'varchar'
        ),
	'created_at' => array(
            'data_type' => 'int'
        ),
	'updated_at' => array(
            'data_type' => 'int'
        ),
    );
    
    public static function validate($factory) {
        $validation = Validation::forge($factory);
        
        switch($factory)
        {
            case "register": {
                $validation->add_field('Username', 'Lietotājvārds', 'required|max_length[30]|min_length[3]');
                $validation->add_field('Firstname', 'Vārds', 'max_length[30]|min_length[2]');
                $validation->add_field('Lastname', 'Uzvārds', 'max_length[30]|min_length[2]');
                $validation->add_field('Email', 'E-pasts', 'max_length[255]|min_length[3]|valid_email');
                $validation->add_field('Password1', 'Parole', 'required|max_length[16]|min_length[4]');
                $validation->add_field('Password2', 'Parole atkārtoti', 'required|max_length[16]|min_length[4]');
            
                break;
            }
            case "change_info": {
                $validation->add_field('Firstname', 'Vārds', 'max_length[30]|min_length[2]');
                $validation->add_field('Lastname', 'Uzvārds', 'max_length[30]|min_length[2]');
                $validation->add_field('Email', 'E-pasts', 'max_length[255]|min_length[3]|valid_email');     
            
                break;
            }
            case "change_password": {
                $validation->add_field('CurrentPassword', 'Parole', 'required|max_length[16]|min_length[4]');
                $validation->add_field('NewPassword1', 'Parole atkārtoti', 'required|max_length[16]|min_length[4]');  
                $validation->add_field('NewPassword2', 'Parole atkārtoti', 'required|max_length[16]|min_length[4]');   
            
                break;
            }
        }
        
	return $validation;
    }
}

?>
